package com.test.exercise.impl;

import com.test.exercise.Sequence;

/**
 * description: 使用 自定义的 MyThreadLocal 实现 ThreadLocal功能，线程变量独立，互不影响。
 * @version v1.0
 * @author w
 * @date 2018年12月1日下午4:48:05
 **/
public class SequenceC implements Sequence {
	
	private static MyThreadLocal<Integer> container = new MyThreadLocal<Integer>(){
		@Override
		protected Integer initVariable() {
			return 0 ;
		};
	};
	
	@Override
	public int getNum() {
		Integer num = container.get();
		num = num + 1; 
		container.set(num);
		return num;
	}
	
	public static void main(String[] args) {
		Sequence sequence = new SequenceC();
		
		Thread t1 = new ClientThread(sequence);
		Thread t2 = new ClientThread(sequence);
		Thread t3 = new ClientThread(sequence);
		
		t1.start();
		t2.start();
		t3.start();
	}
}
